package org.elucid.chess.view;

import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

import org.elucid.chess.model.Piece;
import org.elucid.chess.piece.Bishop;
import org.elucid.chess.piece.King;
import org.elucid.chess.piece.Knight;
import org.elucid.chess.piece.Pawn;
import org.elucid.chess.piece.Queen;
import org.elucid.chess.piece.Rook;

/**
 * Utilitiy methods for loading Images, etc.
 * 
 * @author george
 *
 */
public class UiUtil {

	
	/** Returns an ImageIcon, or null if the path was invalid. 
	 * @throws IOException */
	protected static BufferedImage createImageIcon(String path) throws IOException {
		URL url = UiUtil.class.getResource(path);  
        BufferedImage image = ImageIO.read(url);
		return image; 
	}
	
	/**
	 * 
	 * @param image
	 * @param scale
	 * @return
	 */
    static Image getScaledImage(BufferedImage image, double scale)  
    {  
        int w = (int)(image.getWidth() * scale);  
        int h = (int)(image.getHeight() * scale);  
        BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);  
        Graphics2D g2 = bi.createGraphics();  
        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,  
                            RenderingHints.VALUE_INTERPOLATION_BILINEAR);  
        //                    RenderingHints.VALUE_INTERPOLATION_BICUBIC);  
        AffineTransform at = AffineTransform.getScaleInstance(scale, scale);  
        g2.drawRenderedImage(image, at);  
        g2.dispose();  
        return bi;  
    }  
   
    /**
     * 
     * @param image
     * @param scale
     * @return
     */
    static ImageIcon getScaledIcon(final Image image, final double scale)  
    {  
        ImageIcon scaledIcon = new ImageIcon(image)  
        {  
			private static final long serialVersionUID = -237999219964634199L;

			public int getIconWidth()  
            {  
                return (int)(image.getWidth(null) * scale);  
            }  
   
            public int getIconHeight()  
            {  
                return (int)(image.getHeight(null) * scale);  
            }  
   
            public void paintIcon(Component c, Graphics g, int x, int y)  
            {  
                g.drawImage(image, x, y, getIconWidth(), getIconHeight(), c);  
            }  
        };  
        return scaledIcon;  
    }

    /**
     * 
     * @param piece
     * @return
     */
	public static String getPieceImageURL(Piece piece) {
		String url = null;
		if( piece instanceof Pawn )
		{
			if( piece.isWhite() )
				url = "img/white_pawn.png";
			else
				url = "img/black_pawn.png";
		}
		else if( piece instanceof Rook )
		{
			if( piece.isWhite() )
				url = "img/white_rook.png";
			else
				url = "img/black_rook.png";
		}
		else if( piece instanceof Knight )
		{
			if( piece.isWhite() )
				url = "img/white_knight.png";
			else
				url = "img/black_knight.png";
		}
		else if( piece instanceof Bishop )
		{
			if( piece.isWhite() )
				url = "img/white_bishop.png";
			else
				url = "img/black_bishop.png";
		}
		else if( piece instanceof King )
		{
			if( piece.isWhite() )
				url = "img/white_king.png";
			else
				url = "img/black_king.png";
		}
		else if( piece instanceof Queen )
		{
			if( piece.isWhite() )
				url = "img/white_queen.png";
			else
				url = "img/black_queen.png";
		}		
		
		return url;
	}  
}
